"""
配置管理模块
生产环境配置
"""
import os
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()


class Config:
    """生产环境配置类"""
    # Flask 配置
    DEBUG = False
    SECRET_KEY = os.getenv('SECRET_KEY', 'dev-secret-key')
    JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY', 'dev-jwt-secret-key')
    
    # 时区配置
    TIMEZONE = os.getenv('TIMEZONE', 'Asia/Shanghai')  # 默认东8区
    
    # 数据库配置
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    
    # 检查必需的数据库环境变量
    required_db_vars = ['MYSQL_USER', 'MYSQL_PASSWORD', 'MYSQL_HOST', 'MYSQL_PORT', 'MYSQL_DATABASE']
    missing_vars = [var for var in required_db_vars if not os.getenv(var)]
    if missing_vars:
        raise ValueError(f"缺少必需的数据库环境变量: {', '.join(missing_vars)}")
    
    DATABASE_URL = os.getenv(
        'DATABASE_URL',
        f"mysql+pymysql://{os.getenv('MYSQL_USER')}:{os.getenv('MYSQL_PASSWORD')}"
        f"@{os.getenv('MYSQL_HOST')}:{os.getenv('MYSQL_PORT')}/{os.getenv('MYSQL_DATABASE')}?charset=utf8mb4"
    )
    # 设置SQLAlchemy数据库URI
    SQLALCHEMY_DATABASE_URI = DATABASE_URL
    
    # 服务端口配置
    SERVICE_PORT = int(os.getenv('SERVICE_PORT', 8087))  # 通用服务端口
    WEB_SERVICE_PORT = int(os.getenv('WEB_SERVICE_PORT', 5000))
    INTERNAL_SERVICE_PORT = int(os.getenv('INTERNAL_SERVICE_PORT', 8088))  # 内部服务端口
    SCHEDULER_SERVICE_PORT = int(os.getenv('SCHEDULER_SERVICE_PORT', 5001))
    EXECUTOR_SERVICE_PORT = int(os.getenv('EXECUTOR_SERVICE_PORT', 5002))
    
    # Redis配置
    REDIS_URL = os.getenv('REDIS_URL', 'redis://localhost:8091/0')
    
    # 日志配置
    LOG_LEVEL = os.getenv('LOG_LEVEL', 'DEBUG')  # 默认使用DEBUG级别
    LOG_FILE = os.getenv('LOG_FILE', 'logs/xbyjob.log')
    
    # 管理员配置
    ADMIN_USERNAME = os.getenv('ADMIN_USERNAME', 'admin')
    ADMIN_PASSWORD = os.getenv('ADMIN_PASSWORD', 'admin')
    ADMIN_EMAIL = os.getenv('ADMIN_EMAIL', 'admin@xbyjob.com')
    
    # 任务执行配置
    MAX_CONCURRENT_TASKS = int(os.getenv('MAX_CONCURRENT_TASKS', 10))
    TASK_TIMEOUT = int(os.getenv('TASK_TIMEOUT', 3600))
    RETRY_ATTEMPTS = int(os.getenv('RETRY_ATTEMPTS', 3))
    RETRY_DELAY = int(os.getenv('RETRY_DELAY', 60))  # 重试延迟时间(秒)
    
    # 用户脚本配置
    USER_SCRIPTS_DIR = os.getenv('USER_SCRIPTS_DIR', None)  # 如果为None，则使用默认路径
    
    # 公共方法配置 - 使用相对路径
    COMMON_METHODS_DIR = os.getenv('COMMON_METHODS_DIR', 'common_methods')





def get_config():
    """获取当前环境配置"""
    return Config